package com.china08.yxyapi.api.temp;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DateUtils;
import com.china08.yxyapi.util.RabbitMqSendUtil;

@CrossOrigin
@RestController
public class ResourceAndWeikeStatisticalController {

    private static final Logger log = LoggerFactory.getLogger(ResourceAndWeikeStatisticalController.class);

    /**
     * 2017-05-05/2017-05-06
     * 
     * @param request
     * @param response
     * @param begin
     * @param end
     * @return
     */
    @RequestMapping(value = "/api_yw/temp/resource/and/weike/date/{begin}/{end}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "begin") String begin, @PathVariable(value = "end") String end) {
        log.info("====资源微课MQ开始===");
        log.info("开始时间： " + begin);
        log.info("结束时间： " + end);
        StringBuffer str = new StringBuffer();
        Date begindate = DateToolUtils.parse(begin, DateToolUtils.FORMAT_SHORT_YMD);
        Date enddate = DateToolUtils.parse(end, DateToolUtils.FORMAT_SHORT_YMD);
        if (begindate.after(enddate)) {
            str.append("开始时间不能大于结束时间");
            return str;
        }
        long beginLong = begindate.getTime();
        long endLong = enddate.getTime();
        long day = (endLong - beginLong) / (24 * 60 * 60 * 1000);
        day++;
        for (int i = 0; i < day; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(begindate);
            calendar.add(Calendar.DATE, i);
            Date date = calendar.getTime();
            Integer strDate = DateUtils.formatDate4(date);
            str.append(strDate + "\n");
            // 按照区域统计
            RabbitMqSendUtil.ywStatisticsResourceArea(strDate);
            // 按照学校统计
            RabbitMqSendUtil.ywStatisticsResourceSchool(strDate);
            log.info("统计：" + strDate);
        }
        log.info("===========结束========");
        return str;
    }

}
